﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Framework.Graphics;
using Framework.Map;
using Framework.Graphics.Light;
using Framework.GameObjectManagment;
using Framework.Graphics.Light.Bump;
using Framework.Graphics.Light.Shadow;
using Framework.GameObjectManagment.GameObjects;
using Framework.Physics;

namespace Framework.GameStateManagment.GameScreens
{
    class SinglePlayer : GameScreen
    {
        TileMap _map;
        GameObjectManager _GOM;
        LightManager _LM;
        Texture2D tex, tex_normal, black;
        GraphicsDevice _graphics;
        

        public override void Init(Game game)
        {
            _map = new TileMap();

            _GOM = new GameObjectManager();
            _GOM.Init();

            _graphics = game.GraphicsDevice;

            _LM = new LightManager();
            _LM.Init(game.GraphicsDevice, game);
            //Camera.Init(spriteBatch, new Vector2(graphics.PreferredBackBufferWidth,
            //    graphics.PreferredBackBufferHeight));

            tex = game.Content.Load<Texture2D>(@"Test\floor_tile_02-1024");
            tex_normal = game.Content.Load<Texture2D>(@"Test\floor_tile_02-1024_norm");
            //tex = Content.Load<Texture2D>(@"Test\Robot");
            //tex_normal = Content.Load<Texture2D>(@"Test\RobotN");

            _LM.ShadowManager.LightsList.Add(new Framework.Graphics.Light.Shadow.Light(new Vector2(200,
                200), new LightArea(game.GraphicsDevice, ShadowmapSize.Size256)));
            _LM.ShadowManager.LightsList[0].Color = Color.White;

            _LM.BumpManager.Lights.Add(new PointLight()
                                {
                                    IsEnabled = true,
                                    Color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f),
                                    Power = .3f,
                                    LightDecay = 256,
                                    Position = new Vector3(100, 100, 80)
                                });

            //black = (game.Content.Load<Texture2D>(@"Effect/Black"));

            _map.Init(game.Content, "Test");
        }

        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);
            _map.Init(Content, "Test");
            _GOM.LoadContent(Content);
        }

        public override void Update()
        {
            _GOM.Update();

            #region Old

            //_LM.Reset();
            //_LM.BumpManager.Lights[0].Position = new Vector3(Input.MS.X, Input.MS.Y, 80);
            //_LM.ShadowManager.LightsList[0].Position = new Vector2(Input.MS.X, Input.MS.Y);

            //Camera.SpriteBatch.Begin();
            //_map.DrawAll();
            //Camera.SpriteBatch.End();

            //_map.DrawAll(_LM);
            //_map.DrawNormal(_LM);
            //_map.DrawShadowObjects(_LM);
            

            //_LM.AddShadowObjects(black, new Vector2(450, 425));



            //_LM.OptionalUpdate();
            #endregion


        }

        public override void Draw()
        {
            _LM.Reset();
            _LM.BumpManager.Lights[0].Position = new Vector3(Input.MS.X, Input.MS.Y, 80);
            _LM.ShadowManager.LightsList[0].Position = new Vector2(Input.MS.X, Input.MS.Y);

            _graphics.SetRenderTarget(_LM.BumpColor);
            Camera.SpriteBatch.Begin();
            _map.DrawAll();
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);

            _graphics.SetRenderTarget(_LM.BumpNormal);
            Camera.SpriteBatch.Begin();
            _map.DrawNormal();
            Camera.SpriteBatch.End();
            _graphics.SetRenderTarget(null);

            _LM.Draw(_GOM, _map);
            _GOM.Draw();
        }
    }
}